//
//  ViewUtils.m
//  WitWenZhou
//
//  Created by carlos on 13-3-22.
//  Copyright (c) 2013年 carlosk. All rights reserved.
//

#import "ViewUtils.h"
#import "MBProgressHUD.h"
#import "BaseVC.h"
#import "PhoneUtils.h"
@implementation ViewUtils
static ViewUtils *instance;
//点击了确认按钮
static UIAlertView  *alert;
+(void)getIntance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [ViewUtils alloc];
    });
}

+(void)showAlertWithMsg:(NSString *)mMsg withEvent:(BaseOnClickOne )onClickConfrim{
    [ViewUtils getIntance];
    
    if (alert) {
        [alert dismissWithClickedButtonIndex:0 animated:NO];
        alert = nil;
    }
    
    
    instance.onClickConfrim = onClickConfrim;
    instance.onClickIndex = nil;
    alert = [[UIAlertView alloc]initWithTitle:[COAppTool appName]
                                      message:mMsg
                                     delegate:instance
                            cancelButtonTitle:nil
                            otherButtonTitles:kWConfrim,nil];
    [alert show];
}


//点击了某个按钮
+(void)showAlertWithMsg:(NSString *)mMsg withBtns:(NSArray *)mBtnNames withEvent:(BaseOnClickIndex )onClickIndex
{
    [ViewUtils getIntance];
    
    if (alert) {
        [alert dismissWithClickedButtonIndex:0 animated:NO];
        alert = nil;
    }
    
    instance.onClickIndex = onClickIndex;
    instance.onClickConfrim = nil;
    NSString *cancelBtnTitle = [mBtnNames objectAtIndex:0];
    NSString *btn1 = [mBtnNames objectAtIndex:1];
    NSString *btn2 = nil;
    if (mBtnNames.count > 2) {
        btn2 = [mBtnNames objectAtIndex:2];
    }
    
    alert = [[UIAlertView alloc]initWithTitle:[COAppTool appName]
                                      message:mMsg
                                     delegate:instance
                            cancelButtonTitle:cancelBtnTitle
                            otherButtonTitles:btn1,btn2,nil];
    
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    CLog(@"alertview click %d",buttonIndex);
    if (instance.onClickIndex) {
        instance.onClickIndex(buttonIndex);
    }else if (instance.onClickConfrim){
        instance.onClickConfrim();
    }
}


@end
